package com.app.para;

import android.content.Context;
import android.content.SharedPreferences;

import com.app.tool.FieldTypeUtil;
import com.lf2.Lf_Debug;
import com.out.outpara.OuterAppConfig;


public abstract class AppParamCtrl {
	private static int DEBUG_LEVEL = Lf_Debug.DEBUG_OTHER;
	
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    public void init(Context context){
    	Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "----------------------["+getFileName()+"]----------------------");
        this.preferences = context.getSharedPreferences(getFileName(), 0);
        this.editor = this.preferences.edit();
    }

    public void setStringValue(String key, String value){
        if(this.editor != null)
        {
            this.editor.putString(key, value);   
        }
    }

    public String getStringValue(String key, String defvalue){
        try{
                if(this.preferences != null)
                {
                    String value;
                    if(this.preferences.contains(key)){
                        value = this.preferences.getString(key, defvalue);
                    }
                    else
                    {
                        value = defvalue;
                        setStringValue(key,value);
                        syncValue();
                    }
                    Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(DEBUG_LEVEL), "key:["+key+"]value:["+value+"]");
                    return value;
                }
                else
                {
                    Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(DEBUG_LEVEL), " this.preferences == null");
                }
        }
        catch(Exception e){
                e.printStackTrace();
				Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        }
        return defvalue;
    }

    public void syncValue(){
        if(this.editor != null){
            this.editor.commit();
        }
    }

    public void removeValue(String key){
        if(this.editor != null){
            this.editor.remove(key);
        }
    }

    public void increaseTranceNo(){
        String trace = getStringValue(OuterAppConfig.TRACE_NO, "000001");
        trace = FieldTypeUtil.increaseValue(trace);
        if(Integer.parseInt(trace) == 0){
            trace = FieldTypeUtil.increaseValue(trace);
        }
        setStringValue(OuterAppConfig.TRACE_NO, trace);
        syncValue();
        Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "TRACE_NO:"+trace);
    }
    
    public void increaseBatchNo(){
        String trace = getStringValue(OuterAppConfig.BATCH_NO, "300001");
        trace = FieldTypeUtil.increaseValue(trace);
        if(Integer.parseInt(trace) == 0){
        	trace = "300000";
            trace = FieldTypeUtil.increaseValue(trace);
        }
        setStringValue(OuterAppConfig.BATCH_NO, trace);
        syncValue();
        Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "BATCH_NO:"+trace);
    }
    
    public abstract String getFileName();
}


